
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="Python">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>image_generator &#8212; deepaugment 0.2.0 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for image_generator</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">keras</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>

<span class="kn">from</span> <span class="nn">deepaugment.lib.cutout</span> <span class="k">import</span> <span class="n">cutout_numpy</span>
<span class="kn">from</span> <span class="nn">deepaugment.augmenter</span> <span class="k">import</span> <span class="n">augment_by_policy</span>

<div class="viewcode-block" id="random_flip"><a class="viewcode-back" href="../image_generator.html#image_generator.random_flip">[docs]</a><span class="k">def</span> <span class="nf">random_flip</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Flip the input x horizontally with 50% probability.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mf">0.5</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">fliplr</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">x</span></div>


<div class="viewcode-block" id="zero_pad_and_crop"><a class="viewcode-back" href="../image_generator.html#image_generator.zero_pad_and_crop">[docs]</a><span class="k">def</span> <span class="nf">zero_pad_and_crop</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">amount</span><span class="o">=</span><span class="mi">4</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Zero pad by `amount` zero pixels on each side then take a random crop.</span>
<span class="sd">  Args:</span>
<span class="sd">    img: numpy image that will be zero padded and cropped.</span>
<span class="sd">    amount: amount of zeros to pad `img` with horizontally and verically.</span>
<span class="sd">  Returns:</span>
<span class="sd">    The cropped zero padded img. The returned numpy array will be of the same</span>
<span class="sd">    shape as `img`.</span>
<span class="sd">  &quot;&quot;&quot;</span>
    <span class="n">padded_img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span>
        <span class="p">(</span><span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">amount</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">amount</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
    <span class="p">)</span>
    <span class="n">padded_img</span><span class="p">[</span><span class="n">amount</span> <span class="p">:</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">amount</span><span class="p">,</span> <span class="n">amount</span> <span class="p">:</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">amount</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">img</span>
    <span class="n">top</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="mi">2</span> <span class="o">*</span> <span class="n">amount</span><span class="p">)</span>
    <span class="n">left</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="mi">2</span> <span class="o">*</span> <span class="n">amount</span><span class="p">)</span>
    <span class="n">new_img</span> <span class="o">=</span> <span class="n">padded_img</span><span class="p">[</span><span class="n">top</span> <span class="p">:</span> <span class="n">top</span> <span class="o">+</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">left</span> <span class="p">:</span> <span class="n">left</span> <span class="o">+</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="p">:]</span>
    <span class="k">return</span> <span class="n">new_img</span></div>


<div class="viewcode-block" id="apply_default_transformations"><a class="viewcode-back" href="../image_generator.html#image_generator.apply_default_transformations">[docs]</a><span class="k">def</span> <span class="nf">apply_default_transformations</span><span class="p">(</span><span class="n">X</span><span class="p">):</span>
    <span class="c1"># apply cutout</span>
    <span class="n">X_aug</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">img</span> <span class="ow">in</span> <span class="n">X</span><span class="p">:</span>
        <span class="n">img_aug</span> <span class="o">=</span> <span class="n">zero_pad_and_crop</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">amount</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
        <span class="n">img_aug</span> <span class="o">=</span> <span class="n">cutout_numpy</span><span class="p">(</span><span class="n">img_aug</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
        <span class="n">X_aug</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">img_aug</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">X_aug</span></div>


<div class="viewcode-block" id="deepaugment_image_generator"><a class="viewcode-back" href="../image_generator.html#image_generator.deepaugment_image_generator">[docs]</a><span class="k">def</span> <span class="nf">deepaugment_image_generator</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">policy</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span> <span class="n">augment_chance</span><span class="o">=</span><span class="mf">0.5</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Yields batch of images after applying random augmentations from the policy</span>

<span class="sd">    Each image is augmented by 50% chance. If augmented, one of the augment-chain in the policy is applied.</span>
<span class="sd">    Which augment-chain to apply is chosen randomly.</span>

<span class="sd">    Args:</span>
<span class="sd">        X (numpy.array):</span>
<span class="sd">        labels (numpy.array):</span>
<span class="sd">        policy (list): list of dictionaries</span>

<span class="sd">    Returns:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">policy</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
        <span class="n">policy_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">policy</span><span class="p">)</span>
        <span class="n">policy_df</span> <span class="o">=</span> <span class="n">policy_df</span><span class="p">[</span>
            <span class="p">[</span><span class="s2">&quot;aug1_type&quot;</span><span class="p">,</span> <span class="s2">&quot;aug1_magnitude&quot;</span><span class="p">,</span> <span class="s2">&quot;aug2_type&quot;</span><span class="p">,</span> <span class="s2">&quot;aug2_magnitude&quot;</span><span class="p">,</span> <span class="s2">&quot;portion&quot;</span><span class="p">]</span>
        <span class="p">]</span>
        <span class="n">policy</span> <span class="o">=</span> <span class="n">policy_df</span><span class="o">.</span><span class="n">to_dict</span><span class="p">(</span><span class="n">orient</span><span class="o">=</span><span class="s2">&quot;records&quot;</span><span class="p">)</span>

    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Policies are:&quot;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">policy</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">()</span>

    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
        <span class="n">ix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">X</span><span class="p">))</span>
        <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">ix</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">X</span><span class="p">)</span> <span class="o">//</span> <span class="n">batch_size</span><span class="p">):</span>
            <span class="n">_ix</span> <span class="o">=</span> <span class="n">ix</span><span class="p">[</span><span class="n">i</span> <span class="o">*</span> <span class="n">batch_size</span> <span class="p">:</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">]</span>
            <span class="n">_X</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">_ix</span><span class="p">]</span>
            <span class="n">_y</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="n">_ix</span><span class="p">]</span>

            <span class="n">tiny_batch_size</span> <span class="o">=</span> <span class="mi">4</span>
            <span class="n">aug_X</span> <span class="o">=</span> <span class="n">_X</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">tiny_batch_size</span><span class="p">]</span>
            <span class="n">aug_y</span> <span class="o">=</span> <span class="n">_y</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">tiny_batch_size</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">_X</span><span class="p">)</span> <span class="o">//</span> <span class="n">tiny_batch_size</span><span class="p">):</span>
                <span class="n">tiny_X</span> <span class="o">=</span> <span class="n">_X</span><span class="p">[</span><span class="n">j</span> <span class="o">*</span> <span class="n">tiny_batch_size</span> <span class="p">:</span> <span class="p">(</span><span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">tiny_batch_size</span><span class="p">]</span>
                <span class="n">tiny_y</span> <span class="o">=</span> <span class="n">_y</span><span class="p">[</span><span class="n">j</span> <span class="o">*</span> <span class="n">tiny_batch_size</span> <span class="p">:</span> <span class="p">(</span><span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">tiny_batch_size</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">augment_chance</span><span class="p">:</span>
                    <span class="n">aug_chain</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">policy</span><span class="p">)</span>
                    <span class="n">aug_chain</span><span class="p">[</span>
                        <span class="s2">&quot;portion&quot;</span>
                    <span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>  <span class="c1"># last element is portion, which we want to be 1</span>
                    <span class="n">hyperparams</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">aug_chain</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>

                    <span class="n">aug_data</span> <span class="o">=</span> <span class="n">augment_by_policy</span><span class="p">(</span><span class="n">tiny_X</span><span class="p">,</span> <span class="n">tiny_y</span><span class="p">,</span> <span class="o">*</span><span class="n">hyperparams</span><span class="p">)</span>

                    <span class="n">aug_data</span><span class="p">[</span><span class="s2">&quot;X_train&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">apply_default_transformations</span><span class="p">(</span>
                        <span class="n">aug_data</span><span class="p">[</span><span class="s2">&quot;X_train&quot;</span><span class="p">]</span>
                    <span class="p">)</span>

                    <span class="n">aug_X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">aug_X</span><span class="p">,</span> <span class="n">aug_data</span><span class="p">[</span><span class="s2">&quot;X_train&quot;</span><span class="p">]])</span>
                    <span class="n">aug_y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">aug_y</span><span class="p">,</span> <span class="n">aug_data</span><span class="p">[</span><span class="s2">&quot;y_train&quot;</span><span class="p">]])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">aug_X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">aug_X</span><span class="p">,</span> <span class="n">tiny_X</span><span class="p">])</span>
                    <span class="n">aug_y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">([</span><span class="n">aug_y</span><span class="p">,</span> <span class="n">tiny_y</span><span class="p">])</span>
            <span class="k">yield</span> <span class="n">aug_X</span><span class="p">,</span> <span class="n">aug_y</span></div>


<div class="viewcode-block" id="test_deepaugment_image_generator"><a class="viewcode-back" href="../image_generator.html#image_generator.test_deepaugment_image_generator">[docs]</a><span class="k">def</span> <span class="nf">test_deepaugment_image_generator</span><span class="p">():</span>
    <span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

    <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_categorical</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

    <span class="n">batch_size</span> <span class="o">=</span> <span class="mi">64</span>

    <span class="n">policy</span> <span class="o">=</span> <span class="p">[</span>
        <span class="p">{</span>
            <span class="s2">&quot;aug1_type&quot;</span><span class="p">:</span> <span class="s2">&quot;sharpen&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug1_magnitude&quot;</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">,</span>
            <span class="s2">&quot;aug2_type&quot;</span><span class="p">:</span> <span class="s2">&quot;rotate&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug2_magnitude&quot;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span>
            <span class="s2">&quot;aug3_type&quot;</span><span class="p">:</span> <span class="s2">&quot;emboss&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug3_magnitude&quot;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span>
            <span class="s2">&quot;portion&quot;</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">,</span>
        <span class="p">},</span>
        <span class="p">{</span>
            <span class="s2">&quot;aug1_type&quot;</span><span class="p">:</span> <span class="s2">&quot;gamma-contrast&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug1_magnitude&quot;</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">,</span>
            <span class="s2">&quot;aug2_type&quot;</span><span class="p">:</span> <span class="s2">&quot;dropout&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug2_magnitude&quot;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span>
            <span class="s2">&quot;aug3_type&quot;</span><span class="p">:</span> <span class="s2">&quot;clouds&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug3_magnitude&quot;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span>
            <span class="s2">&quot;portion&quot;</span><span class="p">:</span> <span class="mf">0.2</span><span class="p">,</span>
        <span class="p">},</span>
    <span class="p">]</span>

    <span class="k">return</span> <span class="n">deepaugment_generator</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">policy</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">)</span></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">gen</span> <span class="o">=</span> <span class="n">test_deepaugment_generator</span><span class="p">()</span>

    <span class="n">a</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">gen</span><span class="p">)</span>
    <span class="n">b</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">gen</span><span class="p">)</span>
    <span class="n">c</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">gen</span><span class="p">)</span>

    <span class="k">pass</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">deepaugment</a></h1>








<h3>Navigation</h3>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, Baris Ozmen.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>